<?php
namespace App\Http\Strategy;

use App\Common\ErrorCode\ErrorCode;
use App\Exceptions\BusinessException;
use App\Http\Service\UserService;
use App\Models\UserStaffModel;
use App\Models\JobsModel;
use App\Models\UserStaffOptionModel;
use App\Models\User\UserModel;
use App\Models\JobsCompanysModel;
use App\Models\Admin\JobStaffModel;

class PersonalStrategy
{
    protected $userStaff;
    protected $jobsModel;
    protected $userStaffOptionsModel;
    protected $userModel;
    protected $jobStaffModel;
    protected $jobsCompanyModel;

    public function __construct()
    {
        $this->userStaff = new UserStaffModel();
        $this->jobsModel = new JobsModel();
        $this->userStaffOptionsModel = new UserStaffOptionModel();
        $this->userModel = new UserModel();
        $this->jobStaffModel = new JobStaffModel();
        $this->jobsCompanyModel = new JobsCompanysModel();
    }

    /**
     * 设置员工类型
     * @param $userId
     * @param $cate int 员工类型
     */
    public function setType($userId, $cate)
    {
        $user = $this->userStaff->newQuery()->where(['uid' => $userId])->first();
        $flag = false;
        if (empty($user)) {
            $this->userStaff->setAttribute('uid', $userId);
            $this->userStaff->setAttribute('cate', $cate);
            $this->userStaff->setAttribute('created_at', time());
            $this->userStaff->save();
            $flag = true;
        }else{
            $this->userStaff->setAttribute('updated_at', time());
            $user->setAttribute('cate', $cate);
            $user->save();
            $flag = true;
        }
        return $flag;
    }
    
    public function setJob($uid,$job){
        $user = $this->userStaff->where("uid",$uid)->first();
        $time = time();
        if(!$user){
            $data = ["uid"=>$uid,"job"=>$job,"created_at"=>$time,"updated_at"=>$time];
            if($this->userStaff->insert($data)){
                return true;
            }
            return false;
        }else{
            $data = ["job"=>$job,"updated_at"=>$time];
            if($this->userStaff->where("uid",$uid)->update($data)){
                return true;
            }
            return false;
        }
    }
    
    public function baseInfo($uid,$data){
        $user = $this->userStaff->where("uid",$uid)->first();
        $time = time();
        if(!$user){
            $data["uid"] = $uid;
            $data["created_at"] = $time;
            $data["updated_at"] = $time;
            if($this->userStaff->insert($data)){
                return true;
            }
            return false;
        }else{
            $data["updated_at"] = $time;
            if($this->userStaff->where("uid",$uid)->update($data)){
                return true;
            }
            return false;
        }
    }
    public function getBaseInfo($uid){
        $data = $this->userStaff->where("uid",$uid)->first();
        if(!$data){
            return null;
        }
        $data = $data->toArray();
        $data = $this->modifyBaseInfo($data);
        return $data;
    }
    
    public function modifyBaseInfo($data){
        $cate = $data["cate"];
        $education = $data["education"];
        $political_status = $data["political_status"];
        $marriage = $data["marriage"];
        $job = $data["job"];
        switch ($cate) {
            case 1:
                $data["cate"] = "工厂员工";
                break;
            case 2:
                $data["cate"] = "公司员工";
                break;

            default:
                break;
        }
        //最高学历：1小学；2初中；3高中/中专/专科；4本科；5研究生；6博士；7其他
        switch ($education) {
            case 1:
                $data["education"] = "小学";

                break;
            case 2:
                $data["education"] = "初中";

                break;
            case 3:
                $data["education"] = "高中/中专/专科";

                break;
            case 4:
                $data["education"] = "本科";

                break;
            case 5:
                $data["education"] = "研究生";

                break;
            case 6:
                $data["education"] = "博士";

                break;
            case 7:
                $data["education"] = "其他";

                break;

            default:
                break;
        }
        //政治面貌：1群众；2团员；3党员
        switch ($political_status) {
            case 1:
                $data["political_status"] = "群众";

                break;
            case 2:
                $data["political_status"] = "团员";

                break;
            case 3:
                    $data["political_status"] = "党员";

                break;

            default:
                break;
        }
        //婚姻状态：1未婚；2已婚；3离异
        switch ($marriage) {
            case 1:
                $data["marriage"] = "未婚";

                break;
            case 2:
                $data["marriage"] = "已婚";

                break;
            case 3:
                $data["marriage"] = "离异";

                break;

            default:
                break;
        }
        $jobData = $this->jobsModel->where("id",$job)->first();
        if(!$jobData){
            $data["job"] = "未知职位";
        }else{
            $data["job"] = $jobData->name;
        }
        return $data;
    }
    public function getExtendInfo($uid){
        $data = $this->userStaffOptionsModel->where("uid",$uid)->first();
        if(!$data){
            return null;
        }
        $data = $data->toArray();
        return $data;
    }
    public function setExtendInfo($uid,$data){
        $user = $this->userStaffOptionsModel->where("uid",$uid)->first();
        $time = time();
        if(!$user){
            $data["uid"] = $uid;
            $data["created_at"] = $time;
            $data["updated_at"] = $time;
            if($this->userStaffOptionsModel->insert($data)){
                return true;
            }
            return false;
        }else{
            $data["updated_at"] = $time;
            if($this->userStaffOptionsModel->where("uid",$uid)->update($data)){
                return true;
            }
            return false;
        }
    }
    public function jobWill($uid,$data){
        $user = $this->userStaffOptionsModel->where("uid",$uid)->first();
        $time = time();
        if(!$user){
            $data["created_at"] = $time;
            $data["updated_at"] = $time;
            $data["uid"] = $uid;
            if($this->userStaffOptionsModel->insert($data)){
                return true;
            }
            return false;
        }else{
            $data["updated_at"] = $time;
            if($this->userStaffOptionsModel->where("uid",$uid)->update($data)){
                return true;
            }
            return false;
        }
    }
    public function certification($uid,$data){
        $user = $this->userStaffOptionsModel->where("uid",$uid)->first();
        $time = time();
        if(!$user){
            $data["create_at"] = $time;
            $data["updated_at"] = $time;
            $data["uid"] = $uid;
            if($this->userStaffOptionsModel->insert($data)){
                return true;
            }
            return false;
        }else{
            $data["updated_at"] = $time;
            if($this->userStaffOptionsModel->where("uid",$uid)->update($data)){
                return true;
            }
            return false;
        }
    }
    public function getEducation($uid){
        $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["edu_bgs"]);
        if(!$data){
            return null;
        }
        $data = $data->toArray();
        return $data;
    }
    public function setInfo($uid,$data){
        $data["updated_at"] = time();
        $flag = $this->userStaffOptionsModel->where("uid",$uid)->update($data);
        if(!$flag){
            return false;
        }
        return true;
    }
    /*
    public function setEducation($uid,$data){
        return $this->setInfo($uid, $data);
    }
    */
    public function getWorkHistory($uid){
        $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["work_historys"]);
        if(!$data){
            return null;
        }
        return $data->toArray();
    }
    /*
    public function setWorkHistory($uid,$data){
        return $this->setInfo($uid, $data);
    }
    */
    public function getFamily($uid){
        $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["familys"]);
        if(!$data){
            return null;
        }
        return $data->toArray();
    }
    /*
    public function setFamily($uid,$data){
        return $this->setInfo($uid, $data);
    }
    */
    public function getComment($uid){
       $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["comments"]);
       if(!$data){
           return null;
       }
       return $data->toArray();
    }

   public function getWillFactory($uid){
       $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["will_factorys"]);
        if(!$data){
            return null;
        }
        return $data->toArray();
   }
   public function getWillCompany($uid){
       $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["will_companys"]);
        if(!$data){
            return null;
        }
        return $data->toArray();
   }
   public function getCertification($uid){
       $data = $this->userStaffOptionsModel->where("uid",$uid)->first(["files"]);
        if(!$data){
            return null;
        }
        return $data->toArray();
   }
   public function delPic($uid,$dataPic){
       $flag = $this->userStaffOptionsModel->where("uid",$uid)->update($dataPic);
       if(!$flag){
           return false;
       }
       return true;
   }
   public function saveCertification($uid,$data){
       $data["updated_at"] = time();
       $flag = $this->userStaffOptionsModel->where("uid",$uid)->update($data);
       if(!$flag){
           return false;
       }
       return true;
   }
   public function getUserPhone($uid){
       $data = $this->userModel->where("id",$uid)->first(["phone"]);
       if(!$data){
           return null;
       }
       return $data->toArray();
   }
   public function savePassword($uid,$phone,$password){
       $data["password"] = $password;
       $flag = $this->userModel->where("uid",$uid)->where("phone",$phone)->update($data);
       if(!$flag){
           return false;
       }
       return true;
   }
   public function getAcceptListsCount($uid){
       $data = $this->jobStaffModel->where("uid",$uid)->get();
       if(!$data){
           return 0;
       }
       return count($data->toArray());
   }
   public function getAcceptListsByPage($uid,$pageSize,$page){
       $param = ['id','uid','origin_id','from','status','updated_at'];
       $data = $this->jobStaffModel
               ->where("uid",$uid)
               ->where("del",0)
               ->skip(($page-1)*$pageSize)
               ->take($pageSize)
               ->get($param);
       if(!$data){
           return null;
       }
       $data = $data->toArray();
       $data = $this->getChange($data);
       return $data;
   }
   public function getChange($data){
       foreach($data as &$v){
           $cid = $v["cid"];
           $user = $this->userModel->where("id",$cid)->first(["name"]);
           $v["userName"] = $user["name"];
           switch ($v["from"]) {
               case 1:
                   $v["from"] = "工人投递";

                   break;
               case 2:
                   $v["from"] = "公司邀请";

                   break;

               default:
                   break;
           }
           //状态：1工人投递中，2企业拒绝投递，3企业面试邀请，4工人拒绝邀请，5面试中，6面试失败，7面试成功，8工人评价，9企业评价，10完成
           switch ($v["status"]) {
               case 1:
                   $v["status"] = "工人投递中";

                   break;
               case 2:
                   $v["status"] = "企业拒绝投递";

                    break;
               case 3:
                   $v["status"] = "企业面试邀请";

                    break;
               case 4:
                   $v["status"] = "工人拒绝邀请";

                   break;
               case 5:
                   $v["status"] = "面试中";

                    break;
               case 6:
                   $v["status"] = "面试失败";

                    break;
               case 7:
                   $v["status"] = "面试成功";

                   break;
               case 8:
                   $v["status"] = "工人评价";

                    break;
               case 9:
                   $v["status"] = "企业评价";

                    break;
               case 10:
                   $v["status"] = "完成";

                    break;

               default:
                   break;
           }
       }
       return $data;
   }
   public function acceptInfo($id){
       $data = $this->jobsCompanyModel->where("id",$id)->first();
       if(!$data){
           return null;
       }
       $data = $data->toArray();
       $data = $this->changeInfo($data);
       return $data;
   }
   public function changeInfo($data){
       $cid = $data["cid"];
       $userData = $this->userModel->where("id",$cid)->first();
       $data["cid"] = $userData["name"];
       $jobId = $data["job"];
       $jobData = $this->jobsModel->where("id",$jobId)->first();
       $data["job"] = $jobData["name"];
       switch ($data["welfare"]) {
           case 0:
               $data["welfare"] = "暂无";

               break;
           case 1:
               $data["welfare"] = "包住";

               break;
           case 2:
               $data["welfare"] = "包吃";

               break;

           default:
               break;
       }
       $data["rest"] = "单休";
       if($data["rest"] == 2){
           $data["rest"] = "双休";
       }
       //学历：1小学；2初中；3高中；4大专；5本科；6研究生；7其他；
       switch ($data["education"]) {
           case 1:
               $data["education"] = "小学";

               break;
           case 2:
               $data["education"] = "初中";

               break;
           case 3:
               $data["education"] = "高中";

               break;
           case 4:
               $data["education"] = "大专";

                break;
           case 5:
               $data["education"] = "本科";

               break;
           case 6:
               $data["education"] = "研究生";

               break;
           case 7:
               $data["education"] = "其他";

               break;

           default:
               break;
       }
       //工作年限：1一年以内；2一至二年；3三至五年；4五年以上；
       switch ($data["work_year"]) {
           case 1:
               $data["work_year"] = "一年以内";

               break;
           case 2:
               $data["work_year"] = "一至二年";

               break;
           case 3:
               $data["work_year"] = "三至五年";

               break;
           case 4:
               $data["work_year"] = "五年以上";

               break;

           default:
               break;
       }
       //状态：1草稿，2发布中，3退出发布，4评价
       switch ($data["status"]) {
           case 1:
               $data["status"] = "草稿";

               break;
           case 2:
               $data["status"] = "发布中";

               break;
           case 3:
               $data["status"] = "退出发布";

               break;
           case 4:
               $data["status"] = "评价";

                break;

           default:
               break;
       }
       
       switch ($data["sex"]) {
           case 1:
               $data["sex"] = "男";

               break;
           case 2:
               $data["sex"] = "女";

               break;
           

           default:
               break;
       }
       array_splice($data, 20, 3);
       return $data;
   }
}